﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VehicleMaintenance.Framework;
using VehicleMaintenance.Models;

namespace VehicleMaintenance.Query
{
	public partial class CommonQueryForm : BaseForm
	{
		/// <summary>
		/// 记录当前选择的种类
		/// </summary>
		private string currentKind = "";

		public CommonQueryForm()
		{
			InitializeComponent();

		}

		private void CommonQueryForm_Load(object sender, EventArgs e)
		{
			try
			{
				cboxKind.SelectedIndex = 0; ListView lv = new ListView();
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}		

		private void btnQuery_Click(object sender, EventArgs e)
		{
			currentKind = cboxKind.Text;
			BindPersonOrCar();
		}

		private void BindPersonOrCar()
		{
			if (cboxKind.Text.Equals("客户姓名"))
			{
				if (txtValue.Text.Trim().Length == 0)
				{
					this.ShowInfo("请输入客户姓名"); ;
					txtValue.Focus();
					return;
				}

				CarDBContext db = new CarDBContext();
				var list = db.Customer.Where(p => p.Name.IndexOf(txtValue.Text.Trim()) > -1);
                lboxAll.DisplayMember = "Name";
                lboxAll.ValueMember = "CustomerID";
				lboxAll.DataSource = list.ToList();			

			}
			else if (cboxKind.Text.Equals("车牌号"))
			{
				if (txtValue.Text.Trim().Length == 0)
				{
					this.ShowInfo("请输入车牌号"); ;
					txtValue.Focus();
					return;
				}

				CarDBContext db = new CarDBContext();
				var list = db.CarInfo.Where(p => p.NO.IndexOf(txtValue.Text.Trim()) > -1);
				lboxAll.DataSource = list.ToList();
				lboxAll.DisplayMember = "NO";
				lboxAll.ValueMember = "CarID";
			}
		}

		private void BindDataForPerson(int customerID)
		{
			try
			{
				if (customerID == 0)
				{
					return;
				}

				lvData.Items.Clear();
				lvData.Groups.Clear();

				int num = 0;
				decimal total = 0;

				CarDBContext db = new CarDBContext();
				//根据人找车的维修记录
				var cars = db.CarInfo.Where(p => p.CustomerID == customerID).ToList();
				if (cars != null)
				{
					foreach (var car in cars)
					{
						ListViewGroup group = new ListViewGroup();
						group.Header = car.NO;
						//根据车牌号找维修主单号
						var mainBills = db.MainBill.Where(p => p.CarNO == car.NO).ToList();
						if (mainBills != null)
						{
							foreach (var mainbill in mainBills)
							{
								//根据主单号找详单
								var bills = db.RecipientsBill.Include("Good").Where(p => p.MainBillCode == mainbill.MainBillCode).ToList();
								if (bills != null)
								{
									foreach (var bill in bills)
									{
										ListViewItem lv = new ListViewItem(new string[]{bill.Code,bill.Good.Code,bill.Good.Name,bill.Number.ToString(),
													bill.UnitPrice.ToString(),bill.OfferAmount.ToString(),bill.TotalPrice.ToString(),
													bill.Date,bill.Remark});
										group.Items.Add(lv);
										lvData.Items.Add(lv);

										num += bill.Number;
										total += bill.SalesAmount;
									}
								}
							}
						}
						if (group.Items.Count > 0)
						{
							lvData.Groups.Add(group);
						}
					}
				}
				//根据人找购买记录
				var saleBills = db.SalesBill.Where(p => p.CustomerID == customerID).ToList();
				if (saleBills != null)
				{
					ListViewGroup group = new ListViewGroup();
					group.Header = "购买记录";
					foreach (var item in saleBills)
					{
						ListViewItem lv = new ListViewItem(new string[]{item.Code,item.Good.Code,item.Good.Name,item.Number.ToString(),
													item.UnitPrice.ToString(),item.OfferAmount.ToString(),item.TotalPrice.ToString(),
													item.Date,item.Remark});
						group.Items.Add(lv);
						lvData.Items.Add(lv);
						num += item.Number;
						total += item.SalesAmount;
					}
					if (group.Items.Count > 0)
					{
						lvData.Groups.Add(group);
					}
				}
				//根据人找退货记录
				var returnsBills = db.SalesReturnsBill.Where(p => p.CustomerID == customerID).ToList();
				if (returnsBills != null)
				{
					ListViewGroup group = new ListViewGroup();
					group.Header = "退货记录";
					foreach (var item in returnsBills)
					{
						ListViewItem lv = new ListViewItem(new string[]{item.Code,item.Good.Code,item.Good.Name,item.Number.ToString(),
													item.ReturnValue.ToString(),(item.ReturnValue - item.PurchasePrice).ToString(),item.ReturnPrice.ToString(),
													item.Date,item.Remark});
						group.Items.Add(lv);
						lvData.Items.Add(lv);
						num += item.Number;
						total -= item.ReturnPrice;
					}
					if (group.Items.Count > 0)
					{
						lvData.Groups.Add(group);
					}
				}

				lbNumber.Text = num.ToString();
				lbTotalPrice.Text = total.ToString() + "元";
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		private void BindDataForCar(string carNO)
		{
			try
			{
				if (string.IsNullOrEmpty(carNO))
				{
					return;
				}

				lvData.Items.Clear();
				lvData.Groups.Clear();

				int num = 0;
				decimal total = 0;

				CarDBContext db = new CarDBContext();

				ListViewGroup group = new ListViewGroup();
				group.Header = carNO;
				//根据车牌号找维修主单号
				var mainBills = db.MainBill.Where(p => p.CarNO == carNO).ToList();
				if (mainBills != null)
				{
					foreach (var mainbill in mainBills)
					{
						//根据主单号找详单
						var bills = db.RecipientsBill.Include("Good").Where(p => p.MainBillCode == mainbill.MainBillCode).ToList();
						if (bills != null)
						{
							foreach (var bill in bills)
							{
								ListViewItem lv = new ListViewItem(new string[]{bill.Code,bill.Good.Code,bill.Good.Name,bill.Number.ToString(),
													bill.UnitPrice.ToString(),bill.OfferAmount.ToString(),bill.TotalPrice.ToString(),
													bill.Date,bill.Remark});
								group.Items.Add(lv);
								lvData.Items.Add(lv);

								num += bill.Number;
								total += bill.SalesAmount;
							}
						}
					}
				}
				if (group.Items.Count > 0)
				{
					lvData.Groups.Add(group);
				}

				lbNumber.Text = num.ToString();
				lbTotalPrice.Text = total.ToString() + "元";
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		private void lboxAll_SelectedIndexChanged(object sender, EventArgs e)
		{
			try
			{
				if (currentKind.Equals("客户姓名"))
				{
					BindDataForPerson(Convert.ToInt32(lboxAll.SelectedValue));
				}
				else if (cboxKind.Text.Equals("车牌号"))
				{
                    BindDataForCar(lboxAll.Text.Trim());
				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}
	}
}
